const form = layui.form


invovation()
function invovation(){
    $.ajax({
        type:'GET',
        url : '/my/article/cates',
        success(res){
            console.log(res);
            var htmlstr = template('tpl-table',res)
            $('tbody').html(htmlstr)
        }
    })
}
let addindex= null
$('#btnAddCate').on('click', function() {
   addindex = layer.open({
      type: 1,
      area: ['500px', '250px'],
      title: '添加文章分类',
      content: $('#dialog-add').html()
    })
  })
$('body').on('submit','#form-add',function(e){
    e.preventDefault()

    $.ajax({
        type:'POST',
        url :'/my/article/addcates',
        data : $(this).serialize(),
        success(res){
            console.log(res);
            if(res.status !== 0) return layer.msg('上传失败')
            invovation()
            layer.close(addindex)
        }
    })
})
// let indexEdit = null
$('tbody').on('click','.btn-edit',function(){
    indexEdit = layer.open({
        type: 1,
        area: ['500px', '250px'],
        title: '修改文章分类',
        content: $('#dialog-edit').html()
    })
    let id = $(this).data('id')
    $.ajax({
        method : 'GET',
        url : '/my/article/cates/' + id,
        success(res){
            form.val('form-edit',res.data)
        }
    })
})
$('body').on('submit','#form-edit',function(e){
    e.preventDefault()

    $.ajax({
        method : 'POST',
        url:'/my/article/updatecate',
        data:$(this).serialize(),
        success(res){
            if(res.status !== 0){
                return layer.msg('更新失败')
            }layer.msg('更新')
            layer.close(indexEdit)
            invovation()
        }
    })
})
$('tbody').on('click','.btn-delete',function(){
    const id = $(this).data('id')

    layer.confirm('确认删除',{icon: 3, title: '提示'},function(e){
        $.ajax({
            method:'GET',
            url:'/my/article/deletecate/' + id,
            success(res){
                if(res.status !== 0){
                    layer.msg('删除失败')
                }
                layer.close(e)
                invovation()
            }
        })
    })
})